package com.miaoshare.core.base;

import android.content.Context;

public abstract class BasePresenter<V extends BaseView> {
    protected Context context;
    private V baseView;
    protected BaseModel mModel;


    /**
     * 绑定view
     *
     * @param baseView view
     */
    public void attach(V baseView) {
        this.baseView = baseView;
        context = baseView.getContext();
    }


    /**
     * 解除绑定view，解除model中所有订阅者
     */
    public void detach() {
        baseView = null;
        context = null;
        if(mModel != null) {
            mModel.dispose();
            mModel = null;
        }
    }


    public V getBaseView() {
        return baseView;
    }

    public boolean isAttach() {
        return baseView != null;
    }

    public Context getContext() {
        return context;
    }

    public void showLoadingDialog() {
        if (baseView != null) {
            baseView.showLoadingDialog();
        }
    }

    public void dismissLoadingDialog() {
        if (baseView != null) {
            baseView.dismissLoadingDialog();
        }
    }

    public void showLoadingBar() {
        if (baseView != null) {
            baseView.showLoadingBar();
        }
    }

    public void dismissLoadingBar() {
        if (baseView != null) {
            baseView.dismissLoadingBar();
        }
    }
}
